﻿using BehaviorDesigner.Runtime.Tasks.Unity.UnityTransform;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using static UnityEditor.PlayerSettings;
using UnityEngine.UIElements;

namespace Gameplay.CharacterFramework
{
    public class ProjectileEmitter
    {
        public Role Launcher;

        public GameObject ProjectilePrefab;

        public Projectile CurrentProjectile;

        public ProjectileEmitter(Role launcher, GameObject projectilePrefab) 
        {
            ProjectilePrefab = projectilePrefab;
            Launcher = launcher;
        }

        public void Spwan(GameObject gameObject,Transform parent) 
        {
            var obj=GameObject.Instantiate(ProjectilePrefab,parent);
            if ((!obj.TryGetComponent<Projectile>(out CurrentProjectile)))
            {
                Debug.LogError(ProjectilePrefab.name + "未发现Projectile类脚本");
            }
        }

        public void Shoot(Vector3 direction, float speed) 
        {
            if (CurrentProjectile != null) 
            {
                CurrentProjectile.transform.SetParent(null);
                CurrentProjectile.Shoot(direction,speed);
            }
        }

        public void Shoot(Vector3 pos,Quaternion rotate,Vector3 direction,float speed) 
        {
            var obj = GameObject.Instantiate(ProjectilePrefab, pos, rotate);
            if ((!obj.TryGetComponent<Projectile>(out CurrentProjectile)))
            {
                Debug.LogError(ProjectilePrefab.name + "未发现Projectile类脚本");
            }
            else 
            {
                CurrentProjectile.SetLauncher(Launcher);
                CurrentProjectile.Shoot(direction, speed);
            }
            
        }
    }
}
